package com.kfm.dao;

import com.kfm.entity.User;
import com.kfm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    private DBUtil<User> dbUtil = new DBUtil<>();

    public List<User> selectAll() throws SQLException {
        String sql = "select * from user";
        return dbUtil.queryList(sql, User.class);
    }

    public User selectById(String id ) {
        String sql = "select id, username, name, pic, role, state from user where id = ?";
        try {
            return dbUtil.queryOne(sql, User.class, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public User selectByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select id, username, name, pic, role, state from user where username = ? and password = ?";
        return dbUtil.queryOne(sql, User.class, username, password);
    }

    public int save(User user) throws SQLException {
        // 动态 sql
        StringBuffer columns = new StringBuffer("insert into user(id, username, password ");
        StringBuffer values = new StringBuffer(" values(?, ?, ? ");
        List<Object> params = new ArrayList<>();
        params.add(user.getId());
        params.add(user.getUsername());
        params.add(user.getPassword());

        if (user.getName() != null){
            columns.append(" ,name");
            values.append(" ,?");
            params.add(user.getName());
        }
        if (user.getPic() != null){
            columns.append(" ,pic");
            values.append(" ,?");
            params.add(user.getPic());
        }
        if (user.getRole() != null){
            columns.append(" ,role");
            values.append(" ,?");
            params.add(user.getRole());

        }
        if (user.getState() != null){
            columns.append(" ,state");
            values.append(" ,?");
            params.add(user.getState());
        }

        columns.append(")");
        values.append(")");

        String sql = columns.toString() + values.toString();

        return dbUtil.save(sql, false, params.toArray());
    }

    public int updateById(User user) throws SQLException {
        StringBuffer columns = new StringBuffer("update user set ");
        List<Object> params = new ArrayList<>();

        if (user.getUsername() != null){
            columns.append(" username = ?,");
            params.add(user.getUsername());
        }
        if (user.getPassword() != null){
            columns.append(" password = ?,");
            params.add(user.getPassword());
        }
        if (user.getName() != null){
            columns.append(" name = ?,");
            params.add(user.getName());
        }
        if (user.getPic() != null){
            columns.append(" pic = ?,");
            params.add(user.getPic());
        }
        if (user.getRole() != null){
            columns.append(" role = ?,");
            params.add(user.getRole());
        }
        if (user.getState() != null){
            columns.append(" state = ?,");
            params.add(user.getState());
        }
        String sql = columns.toString();
        if (sql.endsWith(",")){
            sql = sql.substring(0, sql.length() - 1);
        }

        sql = sql + " where id = ?";
        params.add(user.getId());
        return dbUtil.update(sql, params.toArray());
    }


    public List<User> selectByRole(int role) throws SQLException {
        String sql = "select * from user where role = ?";
        return dbUtil.queryList(sql, User.class, role);
    }
}
